package amigos;



import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexManager;

import daos.SessionManager;
import servicios.Operation;


public class ConsultarAmigo implements Operation<String> {

	private String nombreUsuarioInicial;
	private Node startPoint;
	private String usuarioABuscar;
	
	public ConsultarAmigo(String nombreUsuarioInicial, String usuarioABuscar){
		this.nombreUsuarioInicial = nombreUsuarioInicial;
		IndexManager index = SessionManager.getGraphSession().index();
		Index<Node> forNodes = index.forNodes("users");
		this.startPoint = forNodes.query("UserName",this.nombreUsuarioInicial).getSingle();
		this.usuarioABuscar = usuarioABuscar;
	}
	
	public String execute() {
		Node returnNode = null;
		for(Relationship r: this.startPoint.getRelationships(Direction.BOTH, TipoRelacion.AMIGO)){
			Node nAux = r.getOtherNode(this.startPoint);
			if (nAux.getProperty("UserName") == this.usuarioABuscar){
				returnNode = nAux;
				break;
			}
			
		}
			if (returnNode == null) throw new RuntimeException("No se encuentra el amigo");
			return (String) returnNode.getProperty("UserName");
		
	}
}
